package com.huawei.hianalytics.f.g;

import android.content.Context;
import com.huawei.hianalytics.g.b;
import com.huawei.hianalytics.util.a;
import com.huawei.hianalytics.util.c;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class e {
    private static final Object a = new Object();

    public static String a(Context context, String str) {
        FileInputStream fileInputStream;
        Throwable th;
        if (!c(context, str)) {
            return null;
        }
        synchronized (a) {
            FileInputStream openFileInput;
            try {
                a aVar = new a(2048);
                openFileInput = context.openFileInput(d(context, str));
                try {
                    byte[] bArr = new byte[2048];
                    while (true) {
                        int read = openFileInput.read(bArr);
                        if (((long) read) == -1) {
                            break;
                        }
                        aVar.a(bArr, read);
                    }
                    if (aVar.a() == 0) {
                        if (openFileInput != null) {
                            try {
                                openFileInput.close();
                            } catch (IOException e) {
                                b.c("StorageUtil", "IOException happened when getInfoFromFile's FileOutputStream close");
                            }
                        }
                        return null;
                    }
                    String str2 = new String(aVar.b(), "UTF-8");
                    if (openFileInput != null) {
                        try {
                            openFileInput.close();
                        } catch (IOException e2) {
                            b.c("StorageUtil", "IOException happened when getInfoFromFile's FileOutputStream close");
                        }
                    }
                    return str2;
                } catch (FileNotFoundException e3) {
                    fileInputStream = openFileInput;
                    try {
                        b.c("StorageUtil", "getInfoFromFile(): is not found file");
                        if (fileInputStream != null) {
                            try {
                                fileInputStream.close();
                            } catch (IOException e4) {
                                b.c("StorageUtil", "IOException happened when getInfoFromFile's FileOutputStream close");
                            }
                        }
                        return null;
                    } catch (Throwable th2) {
                        th = th2;
                        openFileInput = fileInputStream;
                        if (openFileInput != null) {
                            try {
                                openFileInput.close();
                            } catch (IOException e5) {
                                b.c("StorageUtil", "IOException happened when getInfoFromFile's FileOutputStream close");
                            }
                        }
                        throw th;
                    }
                } catch (IOException e6) {
                    try {
                        b.c("StorageUtil", "getInfoFromFile(): IOException");
                        if (openFileInput != null) {
                            try {
                                openFileInput.close();
                            } catch (IOException e7) {
                                b.c("StorageUtil", "IOException happened when getInfoFromFile's FileOutputStream close");
                            }
                        }
                        return null;
                    } catch (Throwable th3) {
                        th = th3;
                        if (openFileInput != null) {
                            openFileInput.close();
                        }
                        throw th;
                    }
                }
            } catch (FileNotFoundException e8) {
                fileInputStream = null;
                b.c("StorageUtil", "getInfoFromFile(): is not found file");
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                return null;
            } catch (IOException e9) {
                openFileInput = null;
                b.c("StorageUtil", "getInfoFromFile(): IOException");
                if (openFileInput != null) {
                    openFileInput.close();
                }
                return null;
            } catch (Throwable th4) {
                openFileInput = null;
                th = th4;
                if (openFileInput != null) {
                    openFileInput.close();
                }
                throw th;
            }
        }
    }

    public static List<String> a(Context context) {
        return com.huawei.hianalytics.util.b.a(context);
    }

    public static void a(File file) {
        c.a(file);
    }

    public static void b(Context context, String str) {
        synchronized (a) {
            context.deleteFile(d(context, str));
        }
    }

    private static boolean c(Context context, String str) {
        File file = new File(context.getFilesDir(), d(context, str));
        if (file.exists()) {
            if (file.length() <= 5242880) {
                return true;
            }
            b.b("HiAnalytics/event", "v1 cached file size overlarge - file len: %d limitedSize: %d", Long.valueOf(file.length()), Long.valueOf(5242880));
            return false;
        }
        b.b("HiAnalytics/event", "cached file not found");
        return false;
    }

    private static String d(Context context, String str) {
        return "hianalytics_" + str + "_" + context.getPackageName();
    }
}
